package com.joysuch.wwyt.dzhapi.controller;

import com.joysuch.wwyt.dzhapi.exception.ApiException;
import com.joysuch.wwyt.dzhapi.req.ApiTokenReq;
import com.joysuch.wwyt.dzhapi.result.ApiResult;
import com.joysuch.wwyt.dzhapi.service.ApiTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author: JamesZY
 * @date: 2021/9/7
 * @description:token接口
 */
@RestController
@RequestMapping("/api/token")
public class ApiTokenController {
    @Autowired
    private ApiTokenService service;


    @PostMapping("/get")
    public ApiResult getToken(@RequestBody @Valid ApiTokenReq req,
                              HttpServletRequest request) {
        String token = null;
        try {
            token = service.getToken(req, request);
        } catch (ApiException e) {
            return ApiResult.error(e.getCode(), e.getMessage());
        }
        return ApiResult.success(token);
    }

}
